import { Email_found_password, Getlverification, Getrverification, Loginification, Login_, QQ_Login, QQ_User_information, Register } from '../../common/Common'
import { Login_fail, Login_loading, Login_start, Login_success, Register_fail, Register_loading, Register_success, verification_code_fail, Verification_code_loading, Verification_code_success } from '../Type';
import * as QQAPI from 'react-native-qq-lib';
// 注册验证码 --done A
const resister_phone = (phone) => {
    const formdata = new FormData();
    formdata.append('phone', phone);
    return (dispatch) => {
        dispatch({
            type: Verification_code_loading,
            msg: '正在发送'
        })
        fetch(Getrverification, {
            method: 'POST',
            mode: 'cors',
            body: formdata
        })
            .then(res => {
                return res.json()
            })
            .then(res => {
                if (res.code === 200) {
                    dispatch({
                        type: Verification_code_success,
                        msg: res.msg
                    })
                } else {
                    dispatch({
                        type: verification_code_fail,
                        msg: res.msg
                    })
                }
            })
            .catch(res => {
                dispatch({
                    type: verification_code_fail,
                    msg: '发送失败' + res
                })
            })
    }
}

// 登录验证码 --done B
const login_phone_func = (phone) => {
    const formdata = new FormData();
    formdata.append('user_phone', phone);
    return (dispatch) => {
        dispatch({
            type: Verification_code_loading,
            msg: '正在发送'
        })
        fetch(Getlverification, {
            method: 'POST',
            mode: 'cors',
            body: formdata
        })
            .then(res => {
                return res.json()
            })
            .then(res => {
                if (res.code === 200) {
                    dispatch({
                        type: Verification_code_success,
                        msg: res.msg
                    })
                } else {
                    dispatch({
                        type: verification_code_fail,
                        msg: res.msg
                    })
                }
            })
            .catch(res => {
                dispatch({
                    type: verification_code_fail,
                    msg: '发送失败' + res
                })
            })
    }
}

// 邮箱找回密码验证码 --wait
const found_password_email = (email) => {
    const formdata = new FormData();
    formdata.append('email', email);
    return (dispatch) => {
        dispatch({
            type: '',
            msg: ''
        })
        fetch(Email_found_password, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: email
        })
            .then(res => {
                return res.json()
            })
            .then(res => {
                console.log(res);
            })
            .catch(res => {
                console.log(res);
            })
    }
}

// 验证码登录 --done C
const Login_ification = (phone, vifi) => {
    const formdata = new FormData();
    formdata.append('phone', phone);
    formdata.append('vifi', vifi);
    return (dispatch) => {
        dispatch({
            type: Login_loading,
            mag: '正在登陆'
        })
        fetch(Loginification, {
            method: 'POST',
            mode: 'cors',
            body: formdata
        })
            .then(res => {
                return res.json()
            })
            .then(res => {
                console.log(res);
                if (res.code === 200) {
                    dispatch({
                        type: Login_success,
                        msg: res.msg,
                        data: res.data
                    })
                } else {
                    dispatch({
                        type: Login_fail,
                        msg: res.msg
                    })
                }
            })
            .catch(res => {
                dispatch({
                    type: Login_fail,
                    msg: '登录失败' + res
                })
            })
    }
}

// 验证码注册--没写状态先不写 --done D 注册的同时完成登录
const Register_func = (phone, vifi) => {
    const formdata = new FormData();
    formdata.append('phone', phone);
    formdata.append('vifi', vifi);
    return (dispatch) => {
        dispatch({
            type: Register_loading,
            msg: '正在注册'
        })
        fetch(Register, {
            method: 'POST',
            mode: 'cors',
            body: formdata
        })
            .then(res => {
                return res.json()
            })
            .then(res => {
                if (res.code === 200) {
                    // 注册要加上登陆吧
                    dispatch({
                        type: Register_success,
                        msg: res.msg,
                        data: res.data
                    })
                    dispatch({
                        type: Login_success,
                        msg: res.msg,
                        data: res.data
                    })
                } else {
                    dispatch({
                        type: Register_fail,
                        msg: res.msg
                    })
                    dispatch({
                        type: Login_fail,
                        msg: res.msg
                    })
                }
            })
            .catch(e => {
                dispatch({
                    type: Register_fail,
                    msg: '注册失败' + e
                })
            })
    }
}
// 账号登录 --done E
const Login_func = (phone, password) => {
    return (dispatch) => {
        const formdata = new FormData();
        formdata.append('user_phone', phone);
        formdata.append('password', password);
        dispatch({
            type: Login_loading,
            msg: '正在登录'
        })
        fetch(Login_, {
            method: 'POST',
            mode: 'cors',
            body: formdata
        })
            .then(res => {
                return res.json()
            })
            .then(res => {
                if (res.code === 200) {
                    console.log(res);
                    dispatch({
                        type: Login_success,
                        msg: res.msg,
                        data: res.data
                    })
                } else {
                    dispatch({
                        type: Login_fail,
                        msg: res.msg
                    })
                }
            })
            .catch(res => {
                dispatch({
                    type: Login_fail,
                    msg: '登陆失败,失败原因' + res
                })
            })
    }
}

// 邮箱找回密码--wait
// 邮箱修改密码 --wait

// QQ登录+注册
const Login_func_qq = () => {
    let scopes = 'get_user_info';
    let formdata = new FormData();
    return (dispatch) => {
        dispatch({
            type: Login_loading,
            msg: '正在登陆'
        })
        QQAPI.isQQInstalled().then(e => {
            if (e) {
                QQAPI.login(scopes)
                    .then(res => {
                        if (res.errCode === 0) {
                            fetch(QQ_User_information + '?access_token=' + res.access_token + '&openid=' + res.openid + '&oauth_consumer_key=' + res.oauth_consumer_key)
                                .then(e => {
                                    return e.json();
                                })
                                .then(e => {
                                    if (e.ret === 0) {
                                        formdata.append('user_name', e.nickname);
                                        formdata.append('user_pic', e.figureurl_qq);
                                        formdata.append('uid', res.openid);
                                        fetch(QQ_Login, {
                                            method: 'POST',
                                            mode: 'cors',
                                            body: formdata
                                        })
                                            .then(e => {
                                                return e.json()
                                            })
                                            .then(e => {
                                                if (e.code === 200) {
                                                    dispatch({
                                                        type: Login_success,
                                                        msg: e.msg,
                                                        data: e.data
                                                    })
                                                } else {
                                                    dispatch({
                                                        type: Login_fail,
                                                        msg: e.msg
                                                    })
                                                }
                                            })
                                            .catch(e => {
                                                dispatch({
                                                    type: Login_fail,
                                                    msg: '登陆失败' + e
                                                })
                                            })
                                    } else {
                                        dispatch({
                                            type: Login_fail,
                                            msg: '登陆失败' + e
                                        })
                                    }
                                })
                                .catch(e => {
                                    dispatch({
                                        type: Login_fail,
                                        msg: '登陆失败' + e
                                    })
                                })
                        }
                    })
            }
        })
    }
}

// 获取唯一标识符


export {
    resister_phone,
    login_phone_func,
    found_password_email,
    Login_ification,
    Register_func,
    Login_func,
    Login_func_qq
}